﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Blank.Extends
{
    public static class RegexExtends
    {
        public static string[] GetRegKeyValues(this string rawString, string regexValue)
        {
            Regex regex = new Regex(regexValue, RegexOptions.IgnoreCase);
            MatchCollection matchs = regex.Matches(rawString);
            string[] strArray = new string[matchs.Count];
            for (int i = 0; i < matchs.Count; i++)
            {
                object[] args = new object[] { matchs[i].Groups["KEY"].Value, matchs[i].Groups["BUHUO"].Value };
                strArray[i] = string.Format("{0}={1}", args);
            }
            return strArray;
        }
        public static string[] GetRegKeyValuesEncode(this string rawString, string regexValue)
        {
            Regex regex = new Regex(regexValue, RegexOptions.IgnoreCase);
            MatchCollection matchs = regex.Matches(rawString);
            string[] strArray = new string[matchs.Count];
            for (int i = 0; i < matchs.Count; i++)
            {
                object[] args = new object[] { HttpUtility.UrlEncode(matchs[i].Groups["KEY"].Value), HttpUtility.UrlEncode(matchs[i].Groups["BUHUO"].Value) };
                strArray[i] = string.Format("{0}={1}", args);
            }
            return strArray;
        }
        public static string GetRegValue(this string rawString, string regexValue)
        {
            Regex regex = new Regex(regexValue, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Match match = regex.Match(rawString);
            if (!match.Success)
            {
                return string.Empty;
            }
            return match.Groups["BUHUO"].Value;
        }
        public static bool GetRegValueIsMatch(this string rawString, string regexValue)
        {
            Regex regex = new Regex(regexValue, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            return regex.IsMatch(rawString);
        }
        public static string GetRegValueMatch(this string rawString, string regexValue)
        {
            Regex regex = new Regex(regexValue, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            return regex.Match(rawString).Value;
        }
        public static IList<string> GetRegValueMatches(this string rawString, string regexValue)
        {
            Regex regex = new Regex(regexValue, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            MatchCollection matchs = regex.Matches(rawString);
            IList<string> list = new List<string>();
            foreach (Match match in matchs)
            {
                list.Add(match.Value);
            }
            return list;
        }
        public static string GetRegValueReplace(this string rawString, string regexValue, string replaceStr)
        {
            Regex regex = new Regex(regexValue, RegexOptions.Multiline | RegexOptions.IgnoreCase);
            return regex.Replace(rawString, replaceStr);
        }
        public static string[] GetRegValues(this string rawString, string regexValue)
        {
            Regex regex = new Regex(regexValue, RegexOptions.IgnoreCase);
            MatchCollection matchs = regex.Matches(rawString);
            string[] strArray = new string[matchs.Count];
            for (int i = 0; i < matchs.Count; i++)
            {
                strArray[i] = matchs[i].Groups["BUHUO"].Value;
            }
            return strArray;
        }
    }
}
